package ru.hashcode.hashdroid;
import ru.hashcode.hashdroid.proxy.Tab;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class HashMainActivity extends TabActivity implements View.OnTouchListener{

	private GestureDetector gd;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ResourcesHandler.setResources(this);
		
		
		TabHost tabHost = getTabHost();
		View view = LayoutInflater.from(this).inflate(R.layout.hash_main, tabHost.getTabContentView(), true);
		view.setOnTouchListener(this);
		//FrameLayout fl = (FrameLayout) findViewById(R.id.hash_main_frame);
		//fl.setOnTouchListener(this);
		//fl.setBackgroundColor(getResources().getColor(R.color.question_text));
		//tabHost.setOnTouchListener(this);
		
		gd = new GestureDetector(getApplicationContext(), new LeftRightGestureListener());	
		
		Intent intentMain = new Intent(this, QuestionListActivity.class).putExtra(QuestionListActivity.extraName, Tab.Main);
		Intent intentQ = new Intent(this, QuestionListActivity.class).putExtra(QuestionListActivity.extraName, Tab.Question);
		Intent intentUnans = new Intent(this, QuestionListActivity.class).putExtra(QuestionListActivity.extraName, Tab.Unanswered);
		
		TabSpec tab1 = tabHost.newTabSpec("main").setIndicator(getString(R.string.tab_main)).setContent(intentMain);
		TabSpec tab2 = tabHost.newTabSpec("question").setIndicator(getString(R.string.tab_question)).setContent(intentQ);
		//TabSpec tab3 = tabHost.newTabSpec("tag").setIndicator(getString(R.string.tab_tags)).setContent(R.id.view3);
		//TabSpec tab4 = tabHost.newTabSpec("user").setIndicator(getString(R.string.tab_users)).setContent(R.id.view1);
		//TabSpec tab5 = tabHost.newTabSpec("badge").setIndicator(getString(R.string.tab_badges)).setContent(R.id.view1);
		TabSpec tab6 = tabHost.newTabSpec("unans").setIndicator(getString(R.string.tab_unans)).setContent(intentUnans);
				
		tabHost.addTab(tab1);
		tabHost.addTab(tab2);
		//tabHost.addTab(tab3);
		//tabHost.addTab(tab4);
		//tabHost.addTab(tab5);
		tabHost.addTab(tab6);	
		
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
    	Log.v("HashDroid", "onTouchEvent");
		return gd.onTouchEvent(event);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
    	Log.v("HashDroid", "onTouch");
		return gd.onTouchEvent(event);
	}
	
	private class LeftRightGestureListener extends GestureDetector.SimpleOnGestureListener {
		
	    private static final int MAJOR_MOVE = 10;
	    
		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
        	Log.v("HashDroid", "onFling");
            int dx = (int) (e2.getX() - e1.getX());
            if (Math.abs(dx) > MAJOR_MOVE && Math.abs(velocityX) > Math.abs(velocityY)) {
                if (velocityX > 0) {
                	//nextClickImpl(null);
                	Log.v("HashDroid", "right");
                } else {
                	//prevClickImpl(null);
                	Log.v("HashDroid", "right");
                }
                return true;
            } else {
                return false;
            }
        }

	}

}
